<template>
    <div class="app-container">
        <el-form :model="queryParams"
                 ref="queryForm"
                 :inline="true"
                 v-show="showSearch"
                 label-width="80px"
        >
            <el-form-item label="是否已读" prop="readStatus">
                <el-select
                    v-model="queryParams.readStatus"
                    placeholder="请选择模版类型"
                    clearable
                >
                    <el-option
                        v-for="item in dict.type.infra_boolean_string"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
                <el-date-picker
                    v-model="queryParams.createTime"
                    value-format="yyyy-MM-dd"
                    format="yyyy 年 MM 月 dd 日"
                    type="daterange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    @click="handleUpdateList"
                > 标记已读
                </el-button>
                <el-button
                    plain
                    size="mini"
                    @click="handleUpdateAll"
                > 全部已读
                </el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--          size="mini"-->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['business:flowCard:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" row-key="id" :data="notifyMessageMyList" @selection-change="handleSelectionChange" ref="tableRef">
            <el-table-column type="selection" :selectable="selectable" :reserve-selection="true"/>
            <el-table-column label="发送人" align="center" prop="templateNickname"/>
            <el-table-column label="发送时间" align="center" prop="readTime" >
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="模版类型" align="center" prop="templateType" >
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.system_notify_template_type" :value="scope.row.templateType"/>
                </template>
            </el-table-column>
            <el-table-column label="内容" align="center" prop="templateContent"/>
            <el-table-column label="是否已读" align="center" prop="readStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.infra_boolean_string" :value="scope.row.readStatus"/>
                </template>
            </el-table-column>
            <el-table-column label="阅读时间" align="center" prop="readTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.readTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="160">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="warning"
                        @click="handleReadOne(scope.row.id)"
                        v-show="!scope.row.readStatus"
                    >
                        已读
                    </el-button>
                    <el-button
                        size="mini"
                        type="primary"
                        @click="handleDetail(scope.row)"
                    >
                        详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <NotifyMessageMyDetail ref="detailRef" @success="getList"></NotifyMessageMyDetail>
    </div>
</template>

<script>

import NotifyMessageMyDetail from './dialog/NotifyMessageMyDetail.vue'
import {getMyNotifyMessagePage, updateAllNotifyMessageRead, updateNotifyMessageRead} from '@/api/system/notity/message'

export default {
    name: 'template',
    dicts: ['infra_boolean_string', 'system_notify_template_type'],
    components: {
        NotifyMessageMyDetail
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 信息表格数据
            notifyMessageMyList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            selectedIds: [],
            // 查询参数
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                readStatus: undefined,
                createTime: []
            },
        }
    },
    created() {
        this.getList()
    },
    methods: {

        getList() {
            this.loading = true
            getMyNotifyMessagePage(this.queryParams).then(response => {
                this.notifyMessageMyList = response.data.list
                this.total = response.data.total
                this.loading = false
            })
        },


        /** 某一行，是否允许选中 */
        selectable(row) {
            return !row.readStatus
        },

        /** 标记全部站内信已读 **/
       async handleUpdateAll() {
            await updateAllNotifyMessageRead()
            this.$message.success('全部已读成功！')
            this.$refs.tableRef.value.clearSelection()
            await this.getList()
        },

        /** 标记一些站内信已读 **/
       async handleUpdateList() {
            if (this.selectedIds.length === 0) {
                return
            }
            this.selectedIds = [1,2]
            await updateNotifyMessageRead(this.selectedIds)
            this.$message.success('批量已读成功！')
            this.$refs.tableRef.clearSelection()
            await this.getList()
        },


        /** 当表格选择项发生变化时会触发该事件  */
        handleSelectionChange(array) {
            this.selectedIds = []
            if (!array) {
                return
            }
            array.forEach((row) => this.selectedIds.push(row.id))
        },

        cancel() {
            this.open = false
            this.reset()
        },


        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        /** 详情操作 */
        handleDetail(data) {
            console.log(data)
            this.$refs.detailRef.open(data)
        },

        /** 标记一条站内信已读 */
        async handleReadOne(id) {
            await updateNotifyMessageRead(id)
            this.$message.success('已读成功！')
            await this.getList()
        },


    }
}
</script>
